import Vue from "vue"
import VueRouter from "vue-router"
import About from "@/pages/About"
import Home from "@/pages/Home"
import Message from "@/pages/Message"
import News from "@/pages/News"
import Detail from "@/pages/Detail"

Vue.use(VueRouter)

const router = new VueRouter({
    mode:'history',
    routes:[
        {
            path:'/about',
            component:About,
            meta:{isAuth:true,title:'关于'}
        },
        {
            path:'/home',
            component:Home,
            meta:{title:'主页'},
            children:[
                {
                    path:'message',
                    component:Message,
                    meta:{isAuth:true,title:'消息'},
                    children:[
                        {
                            name:'detail',
                            path:'detail/:id/:title',
                            component:Detail,
                            meta:{isAuth:true,title:'详情'},
                            props({params}){
                                return{
                                    id:params.id,
                                    title:params.title
                                }
                            },
/*                            beforeEnter:(to,from,next)=>{
                                if (to.meta.isAuth){
                                    if (localStorage.getItem('dev') === 'xccit'){
                                        next()
                                        document.title = to.meta.title
                                    }else{
                                        alert("权限不够,无法查看!")
                                    }
                                }
                            }*/
                        }
                    ]
                },
                {
                    path:'news',
                    component:News,
                    meta:{isAuth:true,title:'新闻'}
                }
            ]
        }
    ]
})
//全局前置-路由守卫(初始化时,路由切换前执行)
/*router.beforeEach((to, from, next)=>{
    if(to.meta.isAuth){
        if (localStorage.getItem("dev") === "xccit"){
            next()
        }else{
            alert("非本开发者,无权限访问！")
        }
    }else{
        next() //放行
    }
})*/
//全局后置-路由守卫(初始化时,路由切换后执行)
/*router.afterEach((to, from)=>{
    //路由切换后,更改网页标题
    document.title = to.meta.title || "初始标题"
})*/

export default router